package com.intelligence.medical.tools;

import com.intelligence.medical.entity.Appointment;
import com.intelligence.medical.service.AppointmentService;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class AppointmentTools {
    @Autowired
    private AppointmentService appointmentService;

    @Tool(name="Appointment Registration", value = "According to the parameters, first execute the tool method queryDepartment to check if an appointment is available, and directly reply to the user whether an appointment can be made. Then, ask the user to confirm all appointment information. The appointment will be processed only after the user confirms. If the user does not provide a specific doctor's name, find a doctor from the vector storage.")
    public String bookAppointment(Appointment appointment){
            // Check if the corresponding appointment record exists in the database.
            Appointment appointmentDB = appointmentService.getOne(appointment);

            if(appointmentDB == null){
                appointment.setId(null);// An ID has been set to prevent large model hallucinations.
                if(appointmentService.save(appointment)){
                    return "The appointment is successful, and the appointment details are returned.";
                }else{
                    return "Appointment failed";
                }
            }

            return "You already have an appointment in the same department at the same time.";
    }

    @Tool(name="Cancel the appointment registration", value = "According to the parameters, check if the appointment exists. If it exists, delete the appointment record and return \"Appointment cancellation successful\"; otherwise, return \"Appointment cancellation failed\".\n")
    public String cancelAppointment(Appointment appointment){
        Appointment appointmentDB = appointmentService.getOne(appointment);
        if(appointmentDB != null){
            // Delete the appointment record
            if(appointmentService.removeById(appointmentDB.getId())){
                return "Appointment cancellation successful";
            }else{
                return "Appointment cancellation failed";
            }
        }
        // Cancellation failed
        return "You have no appointment records. Please check the appointment department and time.";
    }

    @Tool(name = "Check if there are available slots", value="Check for available slots based on the department name, date, time, and doctor, and return the result to the user.")
    public boolean queryDepartment(
            @P(value = "Department Name") String name,
            @P(value = "Date") String date,
            @P(value = "Time, optional values: morning, afternoon") String time,
            @P(value = "Doctor's Name", required = false) String doctorName
    ) {
        System.out.println("Check if there are available slots.");
        System.out.println("Department Name：" + name);
        System.out.println("Date：" + date);
        System.out.println("Time：" + time);
        System.out.println("Doctor's Name：" + doctorName);

        //TODO Maintain doctors' scheduling information:
        //If no doctor's name is specified, check for available doctors for appointment based on other conditions (return true if available, otherwise return false);
        //If a doctor's name is specified, check if the doctor has a schedule (return false if there is no schedule).
        //If there is a schedule, check whether the doctor's scheduled time slots are fully booked (return false if fully booked, and return true if there are available time slots).
        return true;
    }
}
